package com.htg.tdtwebapi;

import android.content.Context;
import android.content.res.AssetManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class TdtUtilRegion {
    private static List<Relation> relations;
    private static List<Province> provinces;
    private static List<City> cities;
    private static List<County> counties;

    private static void initData(Context context) throws IOException {
        //
        if (provinces != null && cities != null && counties != null && relations != null)
            return;
        relations = new ArrayList<>();
        provinces = new ArrayList<>();
        cities = new ArrayList<>();
        counties = new ArrayList<>();
        //
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open("region.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            String[] temp = line.split("\t");
            String provinceName = temp[1];
            String provinceCode = temp[2];
            String cityName = temp[3];
            String cityCode = temp[4];
            String countyName = temp[5];
            String countyCode = temp[6];
            //
            County county = new County();
            county.setName(countyName);
            county.setCode(countyCode);
            City city = new City();
            city.setName(cityName);
            city.setCode(cityCode);
            Province province = new Province();
            province.setName(provinceName);
            province.setCode(provinceCode);
            Relation relation1 = new Relation();
            relation1.setCodeSup(provinceCode);
            relation1.setCodeSub(cityCode);
            Relation relation2 = new Relation();
            relation2.setCodeSup(cityCode);
            relation2.setCodeSub(countyCode);

            provinces.add(province);
            cities.add(city);
            counties.add(county);
            relations.add(relation1);
            relations.add(relation2);


        }
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
        //这个不能关，因为这个assetmanager 在相同上下文市全局的，如果这里关了，其他库可能使用相同上下文的assetmanager就会报错
        //assetManager.close();
    }


    public static Region getSupRegion(Context context, String code) throws IOException {
        //
        initData(context);
        //
        for (Relation relation : relations) {
            String codeSub = relation.getCodeSub();
            if (codeSub.equals(code))
                return getRegionByCode(context, relation.getCodeSup());
        }
        return null;
    }

    public static List<Region> getSubRegions(Context context, String code) throws IOException {
        //
        initData(context);
        //
        List<Region> regions = new ArrayList<>();
        for (Relation relation : relations) {
            String codeSup = relation.getCodeSup();
            if (codeSup.equals(code)) {
                regions.add(getRegionByCode(context, relation.getCodeSub()));
            }
        }
        return regions;
    }

    public static boolean isProvince(Context context, String code) throws IOException {
        //
        initData(context);
        //
        for (Province province : provinces) {
            if (province.getCode().equals(code)) return true;
        }
        return false;
    }

    public static boolean isCity(Context context, String code) throws IOException {
        //
        initData(context);
        //
        for (City city : cities) {
            if (city.getCode().equals(code)) return true;
        }
        return false;
    }

    public static boolean isCount(Context context, String code) throws IOException {
//
        initData(context);
        //
        for (County county : counties) {
            if (county.getCode().equals(code))
                return true;
        }
        return false;
    }


    public static Region getRegionByCode(Context context, String code) throws IOException {
        //
        initData(context);
        //

        Province province = getProvinceByCode(code);
        if (province == null) {
            City city = getCityByCode(code);
            if (city == null) {
                County county = getCountyByCode(code);
                return county;
            } else {
                return city;
            }
        } else {
            return province;
        }
    }

    public static List<Region> getRegionByName(Context context, String name) throws IOException {

        //
        initData(context);
        //
        List<County> countyList = getCountyByName(name);
        List<City> cityList = getCityByName(name);
        Province province = getProvinceByName(name);

        List<Region> regions = new ArrayList<>();
        if (countyList != null && countyList.size() > 0)
            regions.addAll(countyList);
        if (cityList != null && cityList.size() > 0)
            regions.addAll(cityList);
        if (province != null)
            regions.add(province);

        return regions;
    }


    public static class Relation implements Serializable {
        //上級行政区编码
        private String codeSup;
        //下级行政区编码
        private String codeSub;

        public String getCodeSup() {
            return codeSup;
        }

        public void setCodeSup(String codeSup) {
            this.codeSup = codeSup;
        }

        public String getCodeSub() {
            return codeSub;
        }

        public void setCodeSub(String codeSub) {
            this.codeSub = codeSub;
        }

        @Override
        public boolean equals(@Nullable Object obj) {
            if (obj instanceof Relation) {
                Relation other = (Relation) obj;
                if (other.getCodeSup().equals(codeSup) && other.getCodeSub().equals(codeSub)) {
                    return true;
                } else return false;
            } else return false;
        }
    }

    public static class Province extends Region implements Serializable {

    }

    public static class City extends Region implements Serializable {

    }

    public static class County extends Region implements Serializable {

    }

    public static class Region implements Serializable {
        private String name;
        private String code;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        @Override
        public boolean equals(@Nullable Object obj) {
            if (obj instanceof Region) {
                Region other = (Region) obj;
                String code = other.getCode();
                return this.code.equals(code);
            } else {
                return false;
            }
        }
    }


    private static Province getProvinceByCode(String code) {
        if (provinces == null) return null;
        //
        for (int i = 0; i < provinces.size(); i++) {
            Province item = provinces.get(i);
            if (item.getCode().equals(code))
                return item;
        }
        return null;
    }

    private static City getCityByCode(String code) {
        if (cities == null) return null;
        //
        for (int i = 0; i < cities.size(); i++) {
            City item = cities.get(i);
            if (item.getCode().equals(code))
                return item;
        }
        return null;
    }

    private static County getCountyByCode(String code) {
        if (counties == null) return null;
        //
        for (int i = 0; i < counties.size(); i++) {
            County item = counties.get(i);
            if (item.getCode().equals(code))
                return item;
        }
        return null;
    }

    private static Province getProvinceByName(String name) {
        if (provinces == null) return null;
        //
        for (int i = 0; i < provinces.size(); i++) {
            Province province = provinces.get(i);
            String itemName = province.getName();
            if (itemName.equals(name))
                return province;
        }
        return null;
    }

    private static List<City> getCityByName(String name) {
        if (cities == null) return new ArrayList<>();
        //
        List<City> temp = new ArrayList<>();
        for (int i = 0; i < cities.size(); i++) {
            City item = cities.get(i);
            String itemName = item.getName();
            if (itemName.equals(name))
                temp.add(item);
        }
        return temp;
    }

    private static List<County> getCountyByName(String name) {
        if (counties == null) return new ArrayList<>();
        //
        List<County> temp = new ArrayList<>();
        for (int i = 0; i < counties.size(); i++) {
            County item = counties.get(i);
            String itemName = item.getName();
            if (itemName.equals(name))
                temp.add(item);
        }
        return temp;
    }

}
